***************************************************************
* 	8th July 2014
*	This do file calculates the date that they will reach SPA 
*	Updated for 2011 and 2014 pensions act by David Sturrock Sep 2017
***************************************************************
	
	gen spady = y_birth + 65 if sex==1
	replace spady = y_birth + 60 if sex==2
	gen spadm = m_birth
	gen spadd = d_birth

	cap gen dob = mdy(m_birth, d_birth, y_birth)

	label var spadd "Day reach SPA"
	label var spady "Year reach SPA"
	label var spadm "Month reach SPA"

	replace spady = 2010 if dob>td(5Apr1950) & dob<td(6Aug1950) & sex==2
	replace spady = 2011 if dob>td(5Aug1950) & dob<td(6Feb1951) & sex==2
	replace spady = 2012 if dob>td(5Feb1951) & dob<td(6Aug1951) & sex==2
	replace spady = 2013 if dob>td(5Aug1951) & dob<td(6Feb1952) & sex==2
	replace spady = 2014 if dob>td(5Feb1952) & dob<td(6Aug1952) & sex==2
	replace spady = 2015 if dob>td(5Aug1952) & dob<td(6Feb1953) & sex==2
	replace spady = 2016 if dob>td(5Feb1953) & dob<td(6Jun1953) & sex==2 // faster increase to 65 from April 1953 cohort onwards
	replace spady = 2017 if dob>td(5Jun1953) & dob<td(6Sep1953) & sex==2 
	replace spady = 2018 if dob>td(5Sep1953) & dob<td(6Dec1953) & sex==2 
	replace spady = 2019 if dob>td(5Dec1953) & dob<td(6May1954) // increase to 66 applies to both men and women
	replace spady = 2020 if dob>td(5May1954) & dob<td(6Oct1955) // increase to 66 applies to both men and women

	* Months as per 1995 Act *
	replace spadm = 5 if ((m_birth==4 & d_birth>5) | (m_birth==5 & d_birth<6)) & dob>td(5Apr1950) & dob<td(6Apr1953) & sex==2
	replace spadm = 7 if ((m_birth==5 & d_birth>5) | (m_birth==6 & d_birth<6)) & dob>td(5Apr1950) & dob<td(6Apr1953) & sex==2
	replace spadm = 9 if ((m_birth==6 & d_birth>5) | (m_birth==7 & d_birth<6)) & dob>td(5Apr1950) & dob<td(6Apr1953) & sex==2
	replace spadm = 11 if ((m_birth==7 & d_birth>5) | (m_birth==8 & d_birth<6)) & dob>td(5Apr1950) & dob<td(6Apr1953) & sex==2
	replace spadm = 1 if ((m_birth==8 & d_birth>5) | (m_birth==9 & d_birth<6)) & dob>td(5Apr1950) & dob<td(6Apr1953) & sex==2
	replace spadm = 3 if ((m_birth==9 & d_birth>5) | (m_birth==10 & d_birth<6)) & dob>td(5Apr1950) & dob<td(6Apr1953) & sex==2
	replace spadm = 5 if ((m_birth==10 & d_birth>5) | (m_birth==11 & d_birth<6)) & dob>td(5Apr1950) & dob<td(6Apr1953) & sex==2
	replace spadm = 7 if ((m_birth==11 & d_birth>5) | (m_birth==12 & d_birth<6)) & dob>td(5Apr1950) & dob<td(6Apr1953) & sex==2
	replace spadm = 9 if ((m_birth==12 & d_birth>5) | (m_birth==1 & d_birth<6)) & dob>td(5Apr1950) & dob<td(6Apr1953) & sex==2
	replace spadm = 11 if ((m_birth==1 & d_birth>5) | (m_birth==2 & d_birth<6)) & dob>td(5Apr1950) & dob<td(6Apr1953) & sex==2
	replace spadm = 1 if ((m_birth==2 & d_birth>5) | (m_birth==3 & d_birth<6)) & dob>td(5Apr1950) & dob<td(6Apr1953) & sex==2
	replace spadm = 3 if ((m_birth==3 & d_birth>5) | (m_birth==4 & d_birth<6)) & dob>td(5Apr1950) & dob<td(6Apr1953) & sex==2
	
	* Months as per 2011 Act - accelerated increase to 65 *
	replace spadm = 7 if ((m_birth == 4  & d_birth >5) | (m_birth ==5  & d_birth <6)) & dob>td(5Apr1953) & dob < td(6Dec1953) & sex==2
	replace spadm = 11 if ((m_birth == 5 & d_birth >5) | (m_birth ==6 & d_birth <6)) & dob>td(5Apr1953) & dob < td(6Dec1953) & sex==2
	replace spadm = 3 if ((m_birth == 6 & d_birth >5) | (m_birth ==7 & d_birth <6)) & dob>td(5Apr1953) & dob < td(6Dec1953) & sex==2
	replace spadm = 7 if ((m_birth == 7  & d_birth >5) | (m_birth == 8 & d_birth <6)) & dob>td(5Apr1953) & dob < td(6Dec1953) & sex==2
	replace spadm = 11 if ((m_birth == 8  & d_birth >5) | ( m_birth == 9 & d_birth <6)) & dob>td(5Apr1953) & dob < td(6Dec1953)	 & sex==2
	replace spadm = 3 if ((m_birth == 9 & d_birth >5) | (m_birth == 10 & d_birth <6)) & dob>td(5Apr1953) & dob < td(6Dec1953)	 & sex==2
	replace spadm = 7 if ((m_birth == 10 & d_birth >5) | (m_birth == 11 & d_birth <6)) & dob>td(5Apr1953) & dob < td(6Dec1953)	 & sex==2
	replace spadm = 11 if (( m_birth == 11 & d_birth >5) | (m_birth == 12 & d_birth <6)) & dob>td(5Apr1953) & dob < td(6Dec1953) & sex==2
		
	* Months as per 2011 Act - increase to 66 *
	replace spadm = 3 if ((m_birth == 12 & d_birth >5) | (m_birth ==1 & d_birth <6)) & dob>td(5Dec1953) & dob < td(6Oct1954)	
	replace spadm = 5 if (( m_birth == 1 & d_birth >5) | (m_birth ==2 & d_birth <6)) & dob>td(5Dec1953) & dob < td(6Oct1954)
	replace spadm = 7 if ((m_birth == 2 & d_birth >5) | (m_birth ==3 & d_birth <6)) & dob>td(5Dec1953) & dob < td(6Oct1954)
	replace spadm = 9 if ((m_birth == 3 & d_birth >5) | (m_birth ==4  & d_birth <6)) & dob>td(5Dec1953) & dob < td(6Oct1954)	
	replace spadm = 11 if ((m_birth == 4  & d_birth >5) | (m_birth ==5 & d_birth <6)) & dob>td(5Dec1953) & dob < td(6Oct1954)
	replace spadm = 1 if ((m_birth == 5 & d_birth >5) | (m_birth ==6 & d_birth <6)) & dob>td(5Dec1953) & dob < td(6Oct1954)
	replace spadm = 3 if (( m_birth == 6 & d_birth >5) | (m_birth ==7 & d_birth <6)) & dob>td(5Dec1953) & dob < td(6Oct1954)	
	replace spadm = 5 if (( m_birth == 7 & d_birth >5) | (m_birth ==8 & d_birth <6)) & dob>td(5Dec1953) & dob < td(6Oct1954)
	replace spadm = 7 if ((m_birth == 8 & d_birth >5) | (m_birth ==9 & d_birth <6)) & dob>td(5Dec1953) & dob < td(6Oct1954)
	replace spadm = 9 if ((m_birth == 9 & d_birth >5) | (m_birth ==10 & d_birth <6)) & dob>td(5Dec1953) & dob < td(6Oct1954)
	
	* Days as per 1995 and 2011 Acts *
	replace spadd = 6 if dob>td(5Apr1950) & dob<td(6Oct1954) & sex==2
	replace spadd = 6 if  dob>td(5Dec1953) & dob < td(6Oct1954) & sex==1
	replace spady = y_birth + 66 if dob>td(5Oct1954) & dob<td(6Apr1960)
	
	* Increase to 67 according to 2014 act *
	replace spady = y_birth + 66 if dob>td(5Apr1960) & dob<td(6Aug1960)
	replace spadm = m_birth + 1 if dob>td(5Apr1960) & dob<td(6May1960)
	replace spadm = m_birth + 2 if dob>td(5May1960) & dob<td(6Jun1960)
	replace spadm = m_birth + 3 if dob>td(5Jun1960) & dob<td(6Jul1960)
	replace spadm = m_birth + 4 if dob>td(5Jul1960) & dob<td(6Aug1960)
	replace spadd = 30 if spadm == 11 & spadd == 31
	
	replace spady = y_birth + 67 if dob>td(5Aug1960) & dob<td(1Jan1961)
	replace spadm = m_birth - 7 if dob>td(5Aug1960) & dob<td(6Sep1960)
	replace spadm = m_birth - 6 if dob>td(5Sep1960) & dob<td(6Oct1960)
	replace spadm = m_birth - 5 if dob>td(5Oct1960) & dob<td(6Nov1960)
	replace spadm = m_birth - 4 if dob>td(5Nov1960) & dob<td(6Dec1960)
	replace spadm = m_birth - 3 if dob>td(5Dec1960) & dob<td(1Jan1961)
	replace spadd = 30 if spadm == 9 & spadd == 31
	
	replace spady = y_birth + 66 if dob>td(31Dec1960) & dob<td(6Feb1961)
	replace spadm = m_birth + 9 if dob>td(31Dec1960) & dob<td(6Jan1961)
	replace spadm = m_birth + 10 if dob>td(5Jan1961) & dob<td(6Feb1961)
	replace spadd = 30 if spadm == 11 & spadd == 31
	
	replace spady = y_birth + 67 if dob>td(5Feb1961) & dob<td(6Mar1961)
	replace spadm = m_birth - 1 if dob>td(5Feb1961) & dob<td(6Mar1961)
	
	replace spady = y_birth + 67 if dob>td(5Mar1961) & dob<td(6Apr1977)
	
	* Increase to 68 according to the 2007 Act. *
	* Will need to be updated for reform announced in 2017 (bringing forward 68) once have schedule for increases. *
	replace spady = 2044 if dob>td(5Apr1977) & dob<td(6Aug1977)
	replace spady = 2045 if dob>td(5Aug1977) & dob<td(6Feb1978)
	replace spady = 2046 if dob>td(5Feb1978) & dob<td(6Apr1978)
	replace spady = y_birth + 68 if dob>td(5Apr1978)
	
	replace spadm = 5 if ((m_birth==4 & d_birth>5) | (m_birth==5 & d_birth<6)) & (dob>td(5Apr1977) & dob<td(6Apr1978))
	replace spadm = 7 if ((m_birth==5 & d_birth>5) | (m_birth==6 & d_birth<6)) & (dob>td(5Apr1977) & dob<td(6Apr1978))
	replace spadm = 9 if ((m_birth==6 & d_birth>5) | (m_birth==7 & d_birth<6)) & (dob>td(5Apr1977) & dob<td(6Apr1978))
	replace spadm = 11 if ((m_birth==7 & d_birth>5) | (m_birth==8 & d_birth<6)) & (dob>td(5Apr1977) & dob<td(6Apr1978))
	replace spadm = 1 if ((m_birth==8 & d_birth>5) | (m_birth==9 & d_birth<6)) & (dob>td(5Apr1977) & dob<td(6Apr1978))
	replace spadm = 3 if ((m_birth==9 & d_birth>5) | (m_birth==10 & d_birth<6)) & (dob>td(5Apr1977) & dob<td(6Apr1978))
	replace spadm = 5 if ((m_birth==10 & d_birth>5) | (m_birth==11 & d_birth<6)) & (dob>td(5Apr1977) & dob<td(6Apr1978))
	replace spadm = 7 if ((m_birth==11 & d_birth>5) | (m_birth==12 & d_birth<6)) & (dob>td(5Apr1977) & dob<td(6Apr1978))
	replace spadm = 9 if ((m_birth==12 & d_birth>5) | (m_birth==1 & d_birth<6)) & (dob>td(5Apr1977) & dob<td(6Apr1978))
	replace spadm = 11 if ((m_birth==1 & d_birth>5) | (m_birth==2 & d_birth<6)) & (dob>td(5Apr1977) & dob<td(6Apr1978))
	replace spadm = 1 if ((m_birth==2 & d_birth>5) | (m_birth==3 & d_birth<6)) & (dob>td(5Apr1977) & dob<td(6Apr1978))
	replace spadm = 3 if ((m_birth==3 & d_birth>5) | (m_birth==4 & d_birth<6)) & (dob>td(5Apr1977) & dob<td(6Apr1978))
	replace spadd = 6 if dob>td(5Apr1977) & dob<td(6Apr1978)

	gen spad = mdy(spadm, spadd, spady)
	format spad %td
	label var spad "Date will reach SPA"

	* Leap years *
	gen LYprob =1 if spad==. & spadm==2 & spadd==29
	replace spadm = 3 if LYprob==1
	replace spadd = 1 if LYprob==1
	replace spad = mdy(spadm, spadd, spady) if spad==.
	drop LYprob dob

